$(document).ready(function(){	
	$("a,input:button,input:submit,button").focus(function(){this.blur();});
	//左导航二级导航数量
	var sub_num=$('#sidebar li.submenu').length;	
	for(var sub_i=0;sub_i<sub_num;sub_i++){
		var submenu=$('#sidebar li.submenu:eq('+sub_i+')');
		var submenu_li=$('#sidebar li.submenu:eq('+sub_i+') ul li').length;
		var label=submenu.find("span.label");
		label.text(submenu_li);	
	};
	/*$(document).on("click",".btn-tab",function(){
		if($(this).hasClass("on")){
			if($(this).hasClass("up")){$(this).removeClass("up");	}
			else{$(this).addClass("up")}
		}else{$(this).addClass("on").siblings(".btn-tab").removeClass("on up")}	
	});*/
	// === Sidebar navigation === //
	//==左导航点击，背景变色事件==//
	$("#sidebar > ul > li > a").click(function(e){
		var _parent=$(this).parent();			
		if(!_parent.hasClass("submenu")){
			_parent.siblings(".submenu").find("li").removeClass("active");			
			_parent.addClass("active").siblings("li").removeClass("active");	
		}	
	});
	$(".submenu >ul >li >a").click(function(e){		
		$(this).parent().addClass("active").siblings().removeClass("active");
		$(this).parents(".submenu").addClass("active").siblings().removeClass("active");
		var li=$(this).parents(".submenu");
		var submenu = $(this).parents('.submenu > ul');
		var submenus = $('#sidebar li.submenu ul');
		var submenus_parents = $('#sidebar li.submenu');		
		/*if(li.hasClass('open'))
		{
			if(($(window).width() > 768) || ($(window).width() < 479)) {
				submenu.slideUp();
			} else {
				submenu.fadeOut(250);
			}
			li.removeClass('open');
		} else 
		{
			if(($(window).width() > 768) || ($(window).width() < 479)) {
				submenus.slideUp();			
				submenu.slideDown();
			} else {
				submenus.fadeOut(250);			
				submenu.fadeIn(250);
			}
			submenus_parents.removeClass('open');		
			li.addClass('open');	
		}*/		
	});
	//==END 左导航点击，背景变色事件==//
	$('.submenu > a').click(function(e)
	{
		e.preventDefault();
		var submenu = $(this).siblings('ul');
		var li = $(this).parents('li');
		var submenus = $('#sidebar li.submenu ul');
		var submenus_parents = $('#sidebar li.submenu');
		if(li.hasClass('open'))
		{
			if(($(window).width() > 768) || ($(window).width() < 479)) {
				submenu.slideUp();
			} else {
				submenu.fadeOut(250);
			}
			li.removeClass('open');
		} else 
		{
			if(($(window).width() > 768) || ($(window).width() < 479)) {
				submenus.slideUp();			
				submenu.slideDown();
			} else {
				submenus.fadeOut(250);			
				submenu.fadeIn(250);
			}
			submenus_parents.removeClass('open');		
			li.addClass('open');	
		}
	});
	
	var ul = $('#sidebar > ul');
	
	$('#sidebar > a').click(function(e)
	{
		e.preventDefault();
		var sidebar = $('#sidebar');
		if(sidebar.hasClass('open'))
		{
			sidebar.removeClass('open');
			ul.slideUp(250);
		} else 
		{
			sidebar.addClass('open');
			ul.slideDown(250);
		}
	});
	
	// === Resize window related === //
	$(window).resize(function()
	{
		if($(window).width() > 479)
		{
			ul.css({'display':'block'});	
			$('#content-header .btn-group').css({width:'auto'});		
		}
		if($(window).width() < 479)
		{
			ul.css({'display':'none'});
			fix_position();
		}
		if($(window).width() > 768)
		{
			$('#user-nav > ul').css({width:'auto',margin:'0'});
            $('#content-header .btn-group').css({width:'auto'});
		}
	});
	
	if($(window).width() < 468)
	{
		ul.css({'display':'none'});
		fix_position();
	}
	
	if($(window).width() > 479)
	{
	   $('#content-header .btn-group').css({width:'auto'});
		ul.css({'display':'block'});
	}
	/*$('a.dropdown-toggle, .dropdown-menu a').on('touchstart', function(e){
  		e.stopPropagation();		
	});*/	
	// === Tooltips === //
	/*$('.tip').tooltip();	
	$('.tip-left').tooltip({ placement: 'left' });	
	$('.tip-right').tooltip({ placement: 'right' });	
	$('.tip-top').tooltip({ placement: 'top' });	
	$('.tip-bottom').tooltip({ placement: 'bottom' });	*/
	
	// === Search input typeahead === //
	/*$('#search input[type=text]').typeahead({
		source: ['Dashboard','Form elements','Common Elements','Validation','Wizard','Buttons','Icons','Interface elements','Support','Calendar','Gallery','Reports','Charts','Graphs','Widgets'],
		items: 4
	});*/
	
	// === Fixes the position of buttons group in content header and top user navigation === //
	function fix_position()
	{
		var uwidth = $('#user-nav > ul').width();
		$('#user-nav > ul').css({width:uwidth,'margin-left':'-' +  '1px'});
        
        //var cwidth = $('#content-header .btn-group').width();
        //$('#content-header .btn-group').css({width:cwidth,'margin-left':'-' + uwidth / 2 + 'px'});
	}
	
	// === Style switcher === //
	/*$('#style-switcher i').click(function()
	{
		if($(this).hasClass('open'))
		{
			$(this).parent().animate({marginRight:'-=190'});
			$(this).removeClass('open');
		} else 
		{
			$(this).parent().animate({marginRight:'+=190'});
			$(this).addClass('open');
		}
		$(this).toggleClass('icon-arrow-left');
		$(this).toggleClass('icon-arrow-right');
	});*/
	
	/*$('#style-switcher a').click(function()
	{
		var style = $(this).attr('href').replace('#','');
		$('.skin-color').attr('href','css/maruti.'+style+'.css');
		$(this).siblings('a').css({'border-color':'transparent'});
		$(this).css({'border-color':'#aaaaaa'});
	});*/
	
	$('.lightbox_trigger').click(function(e) {
		
		e.preventDefault();
		
		var image_href = $(this).attr("href");
		
		if ($('#lightbox').length > 0) {
			
			$('#imgbox').html('<img src="' + image_href + '" /><p><i class="icon-remove icon-white"></i></p>');
		   	
			$('#lightbox').slideDown(500);
		}
		
		else { 
			var lightbox = 
			'<div id="lightbox" style="display:none;">' +
				'<div id="imgbox"><img src="' + image_href +'" />' + 
					'<p><i class="icon-remove icon-white"></i></p>' +
				'</div>' +	
			'</div>';
				
			$('body').append(lightbox);
			$('#lightbox').slideDown(500);
		}
		
	});
	
	$('#lightbox').live('click', function() { 
		$('#lightbox').hide(200);
	});
		
	//商品分类
	var alert_class_num=0;
	var ullip1=function(){
			if(!$(this).parent("p").parent("div").parent("li").hasClass("on")){
				$(this).parent("p").parent("div").parent("li").addClass("on");	
				$(this).parent("p").parent("div").parent("li").siblings("li").removeClass("on");
			}else{
				$(this).parent("p").parent("div").parent("li").removeClass("on");		
			}			
	},ulsecond_p1=function(event){
		if(!$(event.target).hasClass("icon_add")){
			 if($(this).parents("ul").hasClass("two_stage")){
				var val_1=$(this).parents(".two_stage").parent("li").children("div").children("p").find("q").text(),
				val_2=$(this).text(),
				val_all=val_1+" > "+val_2;
				$(this).parents(".controls").find(".alert_class").val(val_all);
				$(this).parents(".alert_list").hide();
				alert_class_num=0;	 
			 }else{
				$(this).children(".icon_add").click();	 
			}
		}
	};
	
	$(document).on("click",".alert_class",function(event){
		$(document).off("click",".alert_list i.icon_add",ullip1)
		.off("click",".alert_list p",ulsecond_p1);
				
		if(alert_class_num==0){$(this).next(".alert_list").show();alert_class_num=1}
		else{$(this).next(".alert_list").hide();alert_class_num=0};		
		
		$(document).one("click",function(){
			$(".controls .alert_list").hide();
			alert_class_num=0;				
	     });
		 
		 event.stopPropagation();	
		 		
		$(document).on("click",".alert_list i.icon_add",ullip1)		
		.on("click",".alert_list p",ulsecond_p1)
		.on("click",".alert_list",function(event){
			event.stopPropagation();	
		});					
	});	
	//自定义分类(有二级必须选二级分类)
	var alert_class_num2=0;	
	var ullip=function(){
			if(!$(this).parent("p").parent("div").parent("li").hasClass("on")){
				$(this).parent("p").parent("div").parent("li").addClass("on");	
				$(this).parent("p").parent("div").parent("li").siblings("li").removeClass("on");
			}else{
				$(this).parent("p").parent("div").parent("li").removeClass("on");		
			}			
	},ulsecond_p=function(event){
		if(!$(event.target).hasClass("icon_add")&&!$(event.target).hasClass("alert_list2_right")){
			  if($(this).parents("ul").hasClass("two_stage")){
				var val_1=$(this).parents(".two_stage").parent("li").children("div").children("p").find("q").text(),
				val_2=$(this).find("span").text(),
				val_all=val_1+" > "+val_2;
				$(this).parents(".controls").find(".alert_class2").val(val_all);
				$(this).parents(".alert_list2").hide();
				alert_class_num2=0;	 
			 }else{
				var li_lenght=$(this).parents("li").find("ul li").length;
				if(li_lenght<2){				
					var val_all=$(this).find("q").text();
					$(this).parents(".controls").find(".alert_class2").val(val_all);
					$(this).parents(".alert_list2").hide();
					alert_class_num2=0;	
				}else{$(this).children(".icon_add").click()}
		     }
		 }
					
	},nullType=function(){
			var val_null=$(this).find("q").text();
			$(this).parents(".controls").find(".alert_class2").val(val_null);
			$(this).parents(".alert_list2").hide();	
			alert_class_num2=0;			
	};
	
	$(document).on("click",".alert_class2",function(event){
		$(document).off("click",".alert_list2 i.icon_add",ullip)
		.off("click",".alert_list2 li p",ulsecond_p)
		.off("click",".nullType",nullType);
		
		if(alert_class_num2==0){$(this).next(".alert_list2").show();alert_class_num2=1}
		else{$(this).next(".alert_list2").hide();alert_class_num2=0};
		
		$(document).one("click",function(){
			$(".controls .alert_list2").hide();	
			alert_class_num2=0;
	     });
		 
		 event.stopPropagation();	
		 		
		$(document).on("click",".alert_list2 i.icon_add",ullip)		
		.on("click",".alert_list2 li p",ulsecond_p)
		.on("click",".nullType",nullType)
		.on("click",".alert_list2",function(event){
			event.stopPropagation();	
		});				
	});
	//自定义分类(一级分类可选)
	var alert_class_num_jk=0;	
	var ullip_jk=function(){
			if(!$(this).parent("p").parent("div").parent("li").hasClass("on")){
				$(this).parent("p").parent("div").parent("li").addClass("on");	
				$(this).parent("p").parent("div").parent("li").siblings("li").removeClass("on");
			}else{
				$(this).parent("p").parent("div").parent("li").removeClass("on");		
			}			
	},ulsecond_p_jk=function(event){
		if(!$(event.target).hasClass("icon_add")&&!$(event.target).hasClass("alert_list2_right")){
		  if($(this).parents("ul").hasClass("two_stage")){
			var val_1=$(this).parents(".two_stage").parent("li").children("div").children("p").find("q").text(),
			val_2=$(this).children("span").text(),
			val_all=val_1+" > "+val_2;
			$(this).parents(".controls").find(".alert_class_jk").val(val_all);
			$(this).parents(".alert_list_jk").hide();
			alert_class_num_jk=0;	 
		 }else{							
				var val_all=$(this).find("q").text();
				$(this).parents(".controls").find(".alert_class_jk").val(val_all);
				$(this).parents(".alert_list_jk").hide();
				alert_class_num_jk=0;							
		 }
		}
	},nullType_jk=function(){
			var val_null=$(this).find("q").text();
			$(this).parents(".controls").find(".alert_class_jk").val(val_null);
			$(this).parents(".alert_list_jk").hide();	
			alert_class_num_jk=0;			
	};
	$(document).on("click",".alert_class_jk",function(event){
		$(document).off("click",".alert_list_jk i.icon_add",ullip_jk)
		.off("click",".alert_list_jk li p",ulsecond_p_jk)
		.off("click",".nullType",nullType_jk);
		
		if(alert_class_num_jk==0){$(this).next(".alert_list_jk").show();alert_class_num_jk=1}
		else{$(this).next(".alert_list_jk").hide();alert_class_num_jk=0};
		
		$(document).one("click",function(){
			$(".controls .alert_list_jk").hide();	
			alert_class_num_jk=0;
	     });
		 
		 event.stopPropagation();	
		 		
		$(document).on("click",".alert_list_jk i.icon_add",ullip_jk)		
		.on("click",".alert_list_jk li p",ulsecond_p_jk)
		.on("click",".nullType",nullType_jk)
		.on("click",".alert_list_jk",function(event){
			event.stopPropagation();	
		});					
	})
	//仿select .alert_box
	var alert_class_num3=0;
	var ullip3=function(){
			if(!$(this).parent("li").hasClass("on")){
				$(this).parent("li").addClass("on");	
				$(this).parent("li").siblings("li").removeClass("on");
			}else{
				$(this).parent("li").removeClass("on");		
			}			
	},ulsecond_p3=function(){		 
			var val_all=$(this).children("span").text();
			$(this).parents(".controls").find(".alert_class3").val(val_all);
			$(this).parents(".alert_list3").hide();
			alert_class_num3=0;	 				
	};
	
	$(document).on("click",".alert_class3",function(event){
		$(document).off("click",".alert_list3 li p",ullip3)
		.off("click",".alert_list3 p",ulsecond_p3);
				
		if(alert_class_num3==0){$(this).next(".alert_list3").show();alert_class_num3=1}
		else{$(this).next(".alert_list3").hide();alert_class_num3=0};		
		
		$(document).one("click",function(){
			$(".controls .alert_list3").hide();
			alert_class_num3=0;				
	     });
		 
		 event.stopPropagation();	
		 		
		$(document).on("click",".alert_list3 li p",ullip3)		
		.on("click",".alert_list3 p",ulsecond_p3)
		.on("click",".alert_list3",function(event){
			event.stopPropagation();	
		})					
	});	
	//分类管理
	$(document).on("click",".classify-alert",function(){				
		if($(this).parent(".classify-alertBox").hasClass("on")){
			$(this).parent(".classify-alertBox").removeClass("on");
		}else{
			$(".classify-alertBox").removeClass("on");
			$(this).parent(".classify-alertBox").addClass("on");
		};	
	})
	.on("click",function(event){
		var _target=$(event.target).parents();
		if(!_target.hasClass("classify-alertBox")){
			  $(".classify-alertBox").removeClass("on");						  
		}
	});
	function classifyAlertClose(){$(".classify-alertBox").removeClass("on")};//关闭层分类层
	
	//分类管理二级展开、关闭
	$(document).on("click",".classify-cont-top .icon_add",function(){
		if($(this).parents(".classify-cont").hasClass("on")){$(this).parents(".classify-cont").removeClass("on");
		}else{$(this).parents(".classify-cont").addClass("on");}
	});
	//选项卡
    var TimeFn = null,TimeFn2 = null;
	$(document).on("click",".choose_ul_a li",function(){
		var _this=$(this);
		// 取消上次延时未执行的方法
    	clearTimeout(TimeFn);
		TimeFn = setTimeout(function(){
			_this.addClass("on").siblings().removeClass("on");
        },100);					
	})
	.on("dblclick",".choose_ul_a li",function(){
		clearTimeout(TimeFn);
    	//双击事件的执行代码
		$(".choose_ul_b").prepend($(this));
		$(".choose_ul_b li:eq(0)").siblings("li").removeClass("on");
	})
	.on("click",".choose_ul_b li",function(){
		var _this=$(this);
		// 取消上次延时未执行的方法
    	clearTimeout(TimeFn2);
		TimeFn2 = setTimeout(function(){
			_this.addClass("on").siblings().removeClass("on");
        },100);					
	})
	.on("dblclick",".choose_ul_b li",function(){
		clearTimeout(TimeFn2);
    	//双击事件的执行代码
		$(".choose_ul_a").prepend($(this));
		$(".choose_ul_a li:eq(0)").siblings("li").removeClass("on");
	})
	.on("click","#add_btn",function(){
		if($(".choose_ul_a li").hasClass("on")){
			var _index=$(".choose_ul_a li.on").index(),
			_length=$(".choose_ul_a li").length-1;
			$(".choose_ul_b").prepend($(".choose_ul_a li.on"));
			if(_length>1&&_index!=_length){$(".choose_ul_a li:eq("+_index+")").addClass("on");}
			else{$(".choose_ul_a li:eq(0)").addClass("on");}
			$(".choose_ul_b li").removeClass("on");
		}
			
	})
	.on("click","#sub_btn",function(){
		if($(".choose_ul_b li").hasClass("on")){
			var _index=$(".choose_ul_b li.on").index(),
			_length=$(".choose_ul_b li").length-1;
			$(".choose_ul_a").prepend($(".choose_ul_b li.on"));
			if(_length>1&&_index!=_length){$(".choose_ul_b li:eq("+_index+")").addClass("on");}
			else{$(".choose_ul_b li:eq(0)").addClass("on");}
			$(".choose_ul_a li").removeClass("on");
		}
			
	});
	//内容收起、展开功能
	$(document).on("click",".widget-title .toggle",function(){
		$(this).parent(".widget-title").siblings(".widget-content").toggle(300);	
	})
	
});
//图片翻页
function picRoll() {
	$(".swfupload").addClass("btn");
	 var li_num=$("#product_pic li").length;
	 var num=6;//显示数量
	 var ul_width=li_num*84;
	 var one_width=84*num;
	 var ul_left=0;
	 $(".product_pic").css("width",one_width+"px");;
	 if(li_num>num){
		 $(".pic_btn_left").css("display","block");
		 $(".pic_btn_right").css("display","block");
		 $("#product_pic ul").css("width",ul_width+"px");
		 var max_left=ul_width-one_width;
		 $(".pic_btn_left").click(function(){
			 if(ul_left<0){
				 ul_left=ul_left+one_width;
			     $("#product_pic ul").animate({left:ul_left},500);	 
				 }
		 });  
		 $(".pic_btn_right").click(function(){
			 if(ul_left>-max_left){
				 ul_left=ul_left-one_width;
			     $("#product_pic ul").animate({left:ul_left},500);	
				 }
			 
		 });
	 }else{
		 $(".pic_btn_left").css("display","none");
		 $(".pic_btn_right").css("display","none"); 
	 };	   
	$("#product_pic li").mouseover(function(){
		  $(this).children(".setNdelete").animate({bottom:0},100);
	});
	$("#product_pic li").mouseleave(function(){
		  $(this).children(".setNdelete").animate({bottom:-25+"px"},100);
	});
  
}
//上传图片功能函数
function hoverPic(){
	$(".swfupload").addClass("btn");
	$(".hoverpic li").on("mouseover",function(){
			$(this).children(".setNdelete").animate({bottom:0},100);
	});
	$(".hoverpic li").on("mouseleave",function(){
			 $(this).children(".setNdelete").animate({bottom:-25+"px"},100);
	});	
}
//弹窗提示
function noPlayer(id){
	var title="下载提示";
	var info="检查到您没有安装flash player！";
	var href="####";
	var btnleft="下载";
	var btnright="取消";
	var noplayer=
		'<div id="'+id+'" class="modal">' +
			 '<div class="modal-header">' +
				 '<button class="close over" type="button">×</button>' +
				 '<h3>'+title+'</h3>' +
			 '</div>' +
			 '<div class="modal-body">'+
				 '<p>'+info+'</p>'+
			 '</div>'+
			 '<div class="modal-footer"> <a class="btn btn-primary" href="'+href+'">'+btnleft+'</a> <button class="btn over">'+btnright+'</button> </div>'+
		'</div>';
	$(noplayer).appendTo("body");
	$("<div class='modal-backdrop in'></div>").appendTo("body");
	$("#"+id).find(".over").click(function(){
		$("#"+id).remove();	
		$("body .modal-backdrop").remove();	
	});
}
//修改密码
function changePwd(id){
	var title="修改密码";
	var btnleft="确定";
	var btnright="取消";
	var changepwd=
		'<div id="'+id+'" class="modal">' +
			 '<div class="modal-header">' +
				 '<button class="close over" type="button">×</button>' +
				 '<h3>'+title+'</h3>' +
			 '</div>' +
			 '<div class="modal-body form-horizontal"><div class="row-fluid">'+
				 	'<div class="control-group"><label class="control-label">新密码：</label><div class="controls"><input type="password" class="span8 m-wrap"  /></div></div><div class="control-group"><label class="control-label">确认密码：</label><div class="controls"><input type="password" class="span8 m-wrap"  /></div></div>'+
			 '</div></div>'+
			 '<div class="modal-footer"> <button type="button" class="btn btn-primary">'+btnleft+'</button> <button class="btn over" type="button">'+btnright+'</button> </div>'+
		'</div>';
	$(changepwd).appendTo("body");
	$("<div class='modal-backdrop in'></div>").appendTo("body");
	$("#"+id).find(".over").click(function(){
		$("#"+id).remove();	
		$("body .modal-backdrop").remove();	
	});
}
//浏览器后退移除弹窗
function removeModal(){
	if($(".modal").length>0){$(".modal").remove()};
	if($(".modal-backdrop").length>0){$(".modal-backdrop").remove()}	
}
//系统右下角提示
var Timesys = null;
function sysTips(orderId){	
	var systitle="订单消息",
	syscont="您有新的订单，订单号："+orderId+"，请及时处理！"	,
	sysbox='<div class="sys-tips">'+
	'<div class="sys-tips-title"><span>云便利商店-最新消息</span></div>'+
    '<div class="sys-tips-cont">'+
        '<h3>'+systitle+'</h3>'+
        '<p>'+syscont+'</p></div>'+
		'<div class="sys-tips-close">x</div><div class="sys-tips-link"><a href="javascript:load(ddf.order.order_js)">查看</a></div>'+
	'</div>';
	$("body").append(sysbox);
	$(".sys-tips").animate({bottom:0},500);
	clearTimeout(Timesys);
	Timesys = setTimeout(function(){
		$(".sys-tips-close").click();
	},5000);	
	$(".sys-tips-close").click(function(){
		var height=$(this).parents(".sys-tips").outerHeight(),_this=$(this);
		$(this).parents(".sys-tips").animate({bottom:-height},500);
		setTimeout(function(){
			_this.parents(".sys-tips").remove();
		},500);	
		//	
	})
}